﻿//好友问题
var tjcurrentIndex=1;
var jycurrentIndex=1;
var zhncurrentIndex=1;
var quecurrentIndex=1;

var tjover=0;//推荐接口数据状态 0有数据 1没数据
var jyover=0;//经验接口数据状态
var zhnover=0;//召唤你接口数据状态
var queover=0;//召唤你接口数据状态
var pageSize=10;//一页显示几条新闻
//var newsnum=0;//第几条新闻
var readyover=0;//控制页面滚动0正常 1加载中
//跳转方法
function hrefgo(){
    //清除转发者信息 防止返回信息保存
    $.cookie("shareUserId","");
    $.cookie("fromuser","");
    //取消事件冒泡
    var e=arguments.callee.caller.arguments[0]||event; //若省略此句，下面的e改为event，IE运行可以，但是其他浏览器就不兼容
    if (e && e.stopPropagation) {
        // this code is for Mozilla and Opera
        e.stopPropagation();
    } else if (window.event) {
        // this code is for IE
        window.event.cancelBubble = true;
    }
    console.log(e.target.nodeName);
    if(e.target.nodeName==="DIV"){
        var url= e.target.dataset.href;
        var pageid=e.target.dataset.page;
        var euserid=e.target.dataset.euser;
        var eid=url.match(/[0-9]+/)[0];

        $.cookie("pulupagescroll",$(window).scrollTop());//当前滚动距离
        $.cookie("pulupagepage",pageid);//当前显示页面
        $.cookie("quecurrentIndex",quecurrentIndex);//问题 铺路数据页码
        location.href=url;

    }
    //回答按钮
    else if(e.target.nodeName==="A"){
        if(e.target.dataset.href){
            var pageid=e.target.dataset.page;
            $.cookie("pulupagescroll",$(window).scrollTop());//当前滚动距离
            $.cookie("pulupagepage",pageid);//当前显示页面
            $.cookie("quecurrentIndex",quecurrentIndex);//问题 铺路数据页码
            var url= e.target.dataset.href;
            location.href=url;
        }
    }
}



//时间 几天前 几分钟前
var now = new Date().getTime();
function getDateTimeStamp(dateStr){
    dateStr=dateStr.match(/[0-9\-T\:]+/)[0].replace(/T/g,' ');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){ result="刚刚";return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){
        result="" + parseInt(monthC) + "月前";
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else
        result="刚刚";
    return result;
}


//召唤你页面  //按钮
var tuijian=document.getElementById("tuijian"); //推荐
var expbtn=document.getElementById("expbtn");  //经验
var askyou=document.getElementById("askyou");   //召唤你
var quebtn=document.getElementById("quebtn"); //问题

var tuijianlist=document.getElementById("tuijianlist");//推荐页面
var explist=document.getElementById("explist");//经验
var askyoulist=document.getElementById("askyoulist");//召唤你
var quelist=document.getElementById("quelist"); //问题
var loca=location.href;


askyou.onclick=function(){
    $.cookie("pulupagescroll","");//当前滚动距离
    $.cookie("pulupagepage","");//当前显示页面
    $.cookie("tjcurrentIndex","");//推荐数据页码
    $.cookie("zhncurrentIndex","");//召唤你数据页码
    $.cookie("jycurrentIndex","");//经验 铺路数据页码
    $.cookie("quecurrentIndex","");//问题 铺路数据页码
    location.href="questionlistzhnw.html";
};
expbtn.onclick=function(){
    $.cookie("pulupagescroll","");//当前滚动距离
    $.cookie("pulupagepage","");//当前显示页面
    $.cookie("tjcurrentIndex","");//推荐数据页码
    $.cookie("zhncurrentIndex","");//召唤你数据页码
    $.cookie("jycurrentIndex","");//经验 铺路数据页码
    $.cookie("quecurrentIndex","");//问题 铺路数据页码
    location.href="questionlistjy.html";
};

quebtn.onclick=function(){
    $.cookie("pulupagescroll","");//当前滚动距离
    $.cookie("pulupagepage","");//当前显示页面
    $.cookie("tjcurrentIndex","");//推荐数据页码
    $.cookie("zhncurrentIndex","");//召唤你数据页码
    $.cookie("jycurrentIndex","");//经验 铺路数据页码
    $.cookie("quecurrentIndex","");//问题 铺路数据页码
    location.href="questionlisttw.html";
};

//付费分享
var sharepay=document.getElementsByClassName("sharepay");
var sharepaymob=document.getElementsByClassName("sharepaymob")[0];
var cancel3=document.getElementsByClassName("cancel3")[0];
var sharepayword=document.getElementsByClassName("sharepayword")[0];
var askpayword=document.getElementsByClassName("askpayword")[0];
var modalbg=document.getElementsByClassName("modal_bg2")[0];
for(var sh=0;sh<sharepay.length;sh++){
    sharepay[sh].onclick=function(){
        modalbg.style.display="block";
        sharepaymob.style.display="block";
        sharepayword.style.display="block";
        askpayword.style.display="none";
    }
}
cancel3.onclick=function(){
    if(modalbg.style.display=="block"&&sharepaymob.style.display=="block"){
        modalbg.className+=' animate_out';
        sharepaymob.className+=' animate_out';
    }
    timer=setTimeout(changeclass3,100);
};
//付费充值跳转
okgobtn.onclick=function(){
    modalbg.className+=' animate_out';
    sharepaymob.className+=' animate_out';
    location.href="scoredetial.html";
};
function changeclass3(){
    modalbg.className='modal_bg modal_bg';
    sharepaymob.className='action_menu_container sharepaymob';
    clearTimeout();
    timer2=null;
    changedisplay3();
};
function changedisplay3(){
    modalbg.style.display="none";
    sharepaymob.style.display="none";
};
//付费回答
var askpay=document.getElementsByClassName("askpay");
for(var as=0;as<askpay.length;as++){
    askpay[as].onclick=function(){
        modalbg.style.display="block";
        sharepaymob.style.display="block";
        sharepayword.style.display="none";
        askpayword.style.display="block";
    }
}
cancel3.onclick=function(){
    if(modalbg.style.display=="block"&&sharepaymob.style.display=="block"){
        modalbg.className+=' animate_out';
        sharepaymob.className+=' animate_out';
    }
    timer=setTimeout(changeclass3,100);
};
//拿问题数据
function que(quecurrentIndex,pageSize){
    if(queover===0){
        $.ajax({
            type:"get",
            datatype:"json",
            url:ydqmyqueurl+id+"&currentIndex="+quecurrentIndex+"&pageSize="+pageSize,
            success:function(d){
                if(d.Message=="没有数据"||!d.Data|| d.Data.length==0){
                    queover=1;
                    $("#waiting2").css("display","none");
                    $("#nonews2").css("display","block");
                }else if(d.Data&&d.Data.length>0&&d.Data.length<pageSize){
                    $("#waiting2").css("display","none");
                    $("#nonews2").css("display","block");
                }
                var ulid="#quelist";
                datafun(d,ulid,queover);//页面样式一样 通用方法
            }
        })
    }
}
//所有页面方法
function allftn(){
    $().ready(function(){
        if($.cookie("pulupagepage")){
            $("#quelist div.addlistplace div").remove();
            var pagelist=document.getElementsByClassName("HomeMainFeed");
            var navlist=document.getElementsByClassName("HeaderSwitcherItem");
            readyover=1;//控制页面滚动
            //问题
            if($.cookie("quecurrentIndex")!=="NaN"){
                quepageSize=parseFloat($.cookie("quecurrentIndex"))*10;
            }else{
                quepageSize=10;
            }
            que(1,quepageSize);
            quecurrentIndex=parseFloat($.cookie("quecurrentIndex"));
        }
        else{
            if(id){
                que(quecurrentIndex,pageSize)
            }
        }
    });
    //var upanddown=127;
    //$(window).scroll(function(){
    //    $("#refreshbtn").css("display","block");
    //    $(".quicklist").css("display","none");
    //    var mainheight=$(".home_feed").height();
    //    var windowheight=$(window).height();
    //    var what=$(".home_feed").offset().top;
    //    var chajuli=$(".home_feed").offset().top - $(window).scrollTop();
    //    var cz=windowheight-mainheight+50;
    //    if(chajuli<cz&&readyover===0){
    //        quecurrentIndex++;
    //        readyover=1;
    //        que(quecurrentIndex,pageSize);
    //    }
    //    //导航条隐藏显示
    //    var a2=chajuli;
    //    if(a2<upanddown){
    //        upanddown=a2;
    //        //向下滚动
    //        if(chajuli<36){
    //            $(".fixed_header").css({"position":"fixed","top":"-44px"});
    //            $(".wrapper.home_feed").css({"margin-top":"44px"});
    //            $("#page_wrapper").css("margin-top","44px");
    //        }
    //    }
    //    else{
    //        upanddown=a2;
    //        //向上滚动
    //        if(0<chajuli){
    //            $(".fixed_header").css({"position":"absolute","top":"0px"});
    //            $(".wrapper.home_feed").css({"margin-top":"0"});
    //            $("#page_wrapper").css("margin-top","83px");
    //        }
    //    }
    //});
//刷新页面
//    $("#refreshbtn").click(function(){
//        quecurrentIndex=1;
//        $("#waiting2").css("display","block");
//        $("#nonews2").css("display","none");
//        $(".fixed_header").css({"position":"absolute","top":"0px"});
//        $(".wrapper.home_feed").css({"margin-top":"0"});
//        $("#page_wrapper").css("margin-top","127px");
//        $("#quelist .addlistplace div").remove();
//        que(quecurrentIndex,pageSize);
//    });
}
var id;
if($.cookie("MYID")){id=$.cookie("MYID")};
var returnurl=encodeURIComponent("https://www.pulu.com/weixin/questionlisttw.html"); //同意后跳转页面
$(document).ready(function(){
    if(is_weixn()){
        if($.cookie("wxopenid")){
//=========================================================
            //从登录页返回有openid没有id
            if($.cookie("MYID")){
                allftn();
            }
            else{
                location.href="login.html";
            }
//=========================================================
        }
        else{
            //先微信验证获取openid 如果已经授权过了 直接页面截取openid 然后发送请求获取 用户的ID
            var reg=new RegExp("code=[a-zA-Z0-9]*");
            if(window.location.href.match(reg)){
                var code=window.location.href.match(reg);
                $.ajax({
                    type:"GET",
                    url:"GetOpenId.ashx?"+code,
                    async: false,
                    success:function(data){
                        var dat=data;
                        $.cookie("wxod",dat);
                        console.log("已经授权登陆");
                        //发请求获取用户的id
                        $.ajax({
                            type:"GET",
                            url:getwxopenidurl+dat,
                            success:function(d){
                                //如果没有用户id 则证明用户是第一次登陆 保存openid跳转至登录页面
                                if(!d.Data){
                                    $.cookie("wxopenid",dat);
                                    //alert($.cookie("wxopenid"));
                                    location.href="login.html";
                                }else if(d.Data&&d.Data.userId){

                                    $.cookie("wxopenid",d.Data.openId);

                                    $.cookie("MYID",d.Data.userId);
                                    id=d.Data.userId;

//=================================================
                                    allftn();
//=================================================
                                }
                                //如果有用户ID 就把cookies里的用户id替换成获取到的id  在这里控制跳转提示框
                            }
                        })
                    }
                })
            }else{
                location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri="+returnurl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            }
        }
    }else{
        allftn();
    }

})